//
//  MyCreationViewController.m
//  Drawboard
//
//  Created by A-01 on 2020/6/29.
//  Copyright © 2020 Mac. All rights reserved.
//

#import "MyCreationViewController.h"
#import "PictureCollectionViewCell.h"

@interface MyCreationViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, weak)UILabel * noDataLabel;
@property (nonatomic, strong)NSArray * pictureArray;
@property (nonatomic, weak)UICollectionView * collectionView;
@property (nonatomic, weak)UIToolbar *toolbar;
@property (nonatomic, weak)UIView * whiteView;
@property (nonatomic, strong)NSIndexPath * indexPath;

@end

@implementation MyCreationViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.pictureArray = DEF_GET_OBJECT(@"image");
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
    layout.itemSize =CGSizeMake(190, 120);
    layout.minimumLineSpacing = 25;
    layout.minimumInteritemSpacing = 20;
    //设置collectionView滚动方向
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        //2.初始化collectionView
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake((kScreenW - 620) / 2, 70 , 620, 265) collectionViewLayout:layout];
        
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.showsVerticalScrollIndicator = NO;
        //3.注册collectionViewCell
    //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [collectionView registerClass:[PictureCollectionViewCell class] forCellWithReuseIdentifier:@"PictureCollectionViewCell"];
        //4.设置代理
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    self.collectionView = collectionView;
    
    [self.view addSubview:self.collectionView];
    
    
    UILabel * nodataLabel = [UILabel new];
    
    nodataLabel.text = @"No Picture";
    nodataLabel.textColor = ZRBlackColor;
    nodataLabel.font = [UIFont boldSystemFontOfSize:32];
    nodataLabel.hidden = YES;
    
    self.noDataLabel = nodataLabel;
    
    [self.view addSubview:self.noDataLabel];
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    self.indexPath = indexPath;
    [self setupToolBarView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.pictureArray.count == 0) {
        
        self.noDataLabel.hidden = NO;
    }else{
        
        self.noDataLabel.hidden = YES;
    }
    return self.pictureArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    PictureCollectionViewCell *cell = (PictureCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"PictureCollectionViewCell" forIndexPath:indexPath];
    UIImage *image = [UIImage imageWithData:self.pictureArray[indexPath.row]];
    cell.imageView.image = image;
    return cell;
}


- (void)clickAlertDeleteButton{
    
    NSArray * arr = DEF_GET_OBJECT(@"image");
    NSMutableArray * imageArr = [NSMutableArray arrayWithArray:arr];
    [imageArr removeObject:self.pictureArray[self.indexPath.row]];
    
    DEF_SET_OBJECT(imageArr, @"image");
    
    [self clickCloseButton];
    self.pictureArray = imageArr;
    [self.collectionView reloadData];
}


- (void)clickAlertShareButton{
    
    NSString *testToShare = @"My Paintings";
    UIImage *imageToShare = [UIImage imageWithData:self.pictureArray[self.indexPath.row]];
    NSArray *activityItems = @[testToShare,imageToShare];
     UIActivityViewController *activityVc = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    [self presentViewController:activityVc animated:YES completion:nil];
}


- (void)clickCloseButton{
    
    [self.toolbar removeFromSuperview];
    [self.whiteView removeFromSuperview];
}


- (void)setupToolBarView{
    
    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //样式
    toolbar.barStyle = UIBarStyleBlack;//半透明
    //透明度
    toolbar.backgroundColor = ZRBlackColor;
    toolbar.alpha = 0.6f;

    self.toolbar = toolbar;
    [self.navigationController.view addSubview:self.toolbar];
    
    UIView * whiteView = [[UIView alloc] init];
    
    whiteView.backgroundColor = ZRWhiteColor;
    whiteView.userInteractionEnabled = YES;
    whiteView.layer.cornerRadius = 20;
    whiteView.layer.borderWidth = 2;
    whiteView.layer.borderColor = [ZRBlackColor CGColor];
    whiteView.layer.masksToBounds = YES;
    
    self.whiteView = whiteView;
    
    [self.navigationController.view addSubview:self.whiteView];

    
    UIButton * closeButton = [UIButton new];
    
    [closeButton setImage:[UIImage imageNamed:@"cancel"] forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(clickCloseButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.whiteView addSubview:closeButton];
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.backgroundColor = ZRWhiteColor;
    UIImage *image = [UIImage imageWithData:self.pictureArray[self.indexPath.row]];
    imageView.image = image;
    
    [self.whiteView addSubview:imageView];
    
    
    UIButton * saveButton = [UIButton new];
    
    [saveButton setTitle:@"delete" forState:UIControlStateNormal];
    [saveButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [saveButton addTarget:self action:@selector(clickAlertDeleteButton) forControlEvents:UIControlEventTouchUpInside];
    [saveButton setBackgroundColor:[UIColor colorWithHexString:@"EDFF1C"]];
    saveButton.layer.cornerRadius = 15;
    saveButton.layer.borderWidth = 2;
    saveButton.layer.borderColor = [ZRBlackColor CGColor];
    
    [self.whiteView addSubview:saveButton];
    
    
    UIButton * shareButton = [UIButton new];
    
    [shareButton setTitle:@"share" forState:UIControlStateNormal];
    [shareButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [shareButton addTarget:self action:@selector(clickAlertShareButton) forControlEvents:UIControlEventTouchUpInside];
    [shareButton setBackgroundColor:[UIColor colorWithHexString:@"EDFF1C"]];
    shareButton.layer.cornerRadius = 15;
    shareButton.layer.borderWidth = 2;
    shareButton.layer.borderColor = [ZRBlackColor CGColor];
    
    [self.whiteView addSubview:shareButton];
    
    
    [self.whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view);
        make.width.mas_equalTo(510);
        make.height.mas_equalTo(290);
    }];
    
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.whiteView).offset(-15);
        make.top.mas_equalTo(self.whiteView).offset(15);
        make.width.height.mas_equalTo(44);
    }];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.whiteView);
        make.top.mas_equalTo(self.whiteView).offset(45);
        make.height.mas_equalTo(150);
        make.width.mas_equalTo(250);
    }];
    
    [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.whiteView).offset(120);
        make.bottom.mas_equalTo(self.whiteView).offset(-20);
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(45);
    }];

    [shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.whiteView).offset(-120);
        make.centerY.width.height.mas_equalTo(saveButton);
    }];
}


-(void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    
    [self.noDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self.view);
    }];
}

@end
